home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / prlbkxmp.lha / ch6 / START < prev    next >
Text File  |  1991-01-08  |  4KB  |  177 lines

  1. #!/usr/bin/perl
  2.  
  3. $> && die "Not running as root\n";
  4.  
  5. @INC = $INC[$#INC - 1]; # Use only the perl library.
  6. die "Perl library is writable by world!!!\n"
  7.     if $< && -W $INC[0];
  8. die "getopts.pl is writable by world!!!\n"
  9.     if $< && -W "$INC[0]/getopts.pl";
  10. require "getopts.pl";
  11. &Getopts('cst');
  12.  
  13. $wall = !opt_s;
  14.  
  15. $_ = "$0 @ARGV";        # To allow "ln START STARTxyz"
  16.  
  17. $ENV{'IFS'} = '' if $ENV{'IFS'};        # plug sh security hole
  18. $ENV{'PATH'} = '/appl/bin:/bin:/usr/bin:/usr/local/bin';
  19.  
  20. # Which daemon to start?
  21.  
  22. if (/abc/) {
  23.     $sys = 'abc';
  24.     $task = 'abcdaemon';
  25.     if (-f '/local/tmp/NO_ABC') {
  26.     die "Can't start abcdaemon while NO_ABC exists\n";
  27.     }
  28. }
  29.  
  30. elsif (/def/) {
  31.     $sys = 'def';
  32.     $task = 'def';
  33.     if (-f '/local/tmp/NO_DEF') {
  34.     die "Can't start def while NO_DEF exists\n";
  35.     }
  36. }
  37. elsif (/xyz/) {
  38.     $sys = 'xyz';
  39.     $task = 'xyzzy';    # Forks plugh and plover.
  40.     if (-f '/local/tmp/NO_XYZ') {
  41.     die "Can't start lm while NO_XYZ exists\n";
  42.     }
  43. }
  44. elsif (/appl/ || !@ARGV) {
  45.     system $0, 'abc';
  46.     system $0, 'def';
  47.     system $0, 'xyz';
  48.     exit 0;
  49. }
  50. else {
  51.     die "Usage: START subsystem\n";
  52. }
  53.  
  54. # Get info for logging.
  55.  
  56. chop($whoami = `who am i`);
  57. $whoami =~ s/[ \t]+/ /g;
  58.  
  59. chop($tty = `tty`);
  60. $tty = '' if $tty !~ m|/dev/|;
  61.  
  62. # Set real uid to effective uid so core dumps will happen.
  63.  
  64. $< = $>;
  65.  
  66. # Move to directory for core dumps.
  67.  
  68. chdir '/local/tmp' || die "Can't chdir to /local/tmp";
  69.  
  70. # See if they should have run KILL.
  71.  
  72. chop($oldpid = `/bin/cat .pid$sys 2>&1`);
  73. if ($oldpid > 0 && kill 0, $oldpid) {
  74.     die "$sys appears to be running already!\n"
  75.       if `/bin/ps -p$oldpid` =~ /START/;
  76. }
  77.  
  78. print "Any core dump of $task will be put into /local/tmp.\n";
  79.  
  80. # Redirect output to log file.
  81.  
  82. open(STDERR,">/local/tmp/${sys}_err_log")
  83.     || (print STDOUT "Can't open log file\n");
  84. open(STDOUT,">&STDERR");
  85.  
  86. # Log the startup.
  87.  
  88. chop($date = `date`);
  89. $date =~ s/ [A-Z][DS]T 19[89][0-9]//;
  90. open(KILLLOG,">>/local/tmp/${sys}_kill_log");
  91. print KILLLOG $date, ' ', $whoami, " $sys started\n";
  92. close(KILLLOG);
  93. if ($tty) {
  94.     open(KILLLOG,">$tty");
  95.     print KILLLOG $date, ' ', $whoami, " $sys started\n";
  96.     close(KILLLOG);
  97. }
  98.  
  99. if ($pid = fork) {         # Run the rest in background.
  100.     open(PID,">.pid$sys"); # Remember pid for quick check above
  101.     print PID "$pid\n";    # (pid of START, not of $task).
  102.     close(PID);
  103.     sleep(3);              # Give each daemon a chance to start
  104.     exit 0;
  105. }
  106. defined $pid || die "Can't fork: $!\n";
  107.  
  108. $SIG{'TERM'} = 'IGNORE';
  109.  
  110. setpgrp(0,$$);  # Prevent kills from infecting other daemons.
  111.  
  112. # Finally, run the poor daemon and collect its status.
  113.  
  114. if ($opt_t) {
  115.     $st = (system "/usr/bin/trace /appl/bin/$task");
  116. }
  117. else {
  118.     $st = (system "/appl/bin/$task");
  119. }
  120.  
  121. # Now get info necessary for logging.
  122.  
  123. chop($date = `date`);
  124. $date =~ s/ [A-Z][DS]T 19[89][0-9]//;
  125.  
  126. # Rename any core dump to a unique name.
  127.  
  128. if ($st & 128) {
  129.     $tm = join('',unpack("x8 a2 x a2 x a2", $date));
  130.     $tm =~ s/ /0/g;
  131.     rename("/local/tmp/core", "/local/tmp/core.$sys$tm");
  132. }
  133.  
  134. sleep(2);
  135.  
  136. # Log it everywhere that makes sense.
  137.  
  138. $entry = "$date $whoami $sys exit " . ($st >> 8) . ", sig " .
  139.     ($st & 127) . ($st & 128 ? ", core dump\n" : "\n");
  140. open(KILLLOG,">>/local/tmp/${sys}_kill_log");
  141. print KILLLOG $entry;
  142. close(KILLLOG);
  143.  
  144. close(STDERR);
  145. close(STDOUT);
  146. open(STDOUT,">&STDIN");
  147. open(ERR,"/local/tmp/${sys}_err_log");
  148. open(CONSOLE,"/dev/console") if $opt_c;
  149. $tty = '' unless -t;
  150. while (<ERR>) {
  151.     if ($_ eq $last) {
  152.     $repeat = 1;
  153.     while (<ERR>) {
  154.         if ($_ eq $last) {
  155.         ++$repeat;
  156.         }
  157.         else {
  158.         $last = "Last message repeated $repeat time" .
  159.             ($repeat == 1 ? "" : "s") . ".\n";
  160.         print CONSOLE $last if $opt_c;
  161.         print STDOUT $last if $tty;
  162.         }
  163.     }
  164.     }
  165.     print CONSOLE $_ if $opt_c;
  166.     print STDOUT $_ if $tty;
  167.     $last = $_;
  168. }
  169.  
  170. if ($wall) {
  171.     open(KILLLOG,"| wall"); # Might as well share the good news
  172.     print KILLLOG $entry;
  173.     close(KILLLOG);
  174. }
  175.  
  176. unlink "/local/tmp/.pid$sys";
  177.